/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ATEST;

import RunableClass.IndexRunnable;
import file.fileread.Readindexstock;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Properties;
import net.ipaddress.authenticator;

/**
 *
 * @author Anatomy
 */
public class NewClass1 implements Runnable {
/////////////////////////////////////////////////////////    
        static  int              count          =   1;
        static  StringBuffer     content        =   new StringBuffer("");
    private static Thread t9;
                int              count1         =   0;
public  static  int              avg            =   0;
                int              a              =   0;    
public  static  String           datetime;

//////////////////////////////////////////////////////////
public static void main(String[]arg){
  // Thread t1=new Thread(new NewClass1());
   //t1.start();
     IndexRunnable INR=new IndexRunnable();
    t9 = new Thread(INR);
    t9.start();
}

    @Override
    public void run() {
       for(;;){ 
          content = new StringBuffer("");int count2=1;
         
         
          Calendar now = Calendar.getInstance();
            //int h = now.get(Calendar.HOUR_OF_DAY);
            int h = now.get(Calendar.HOUR_OF_DAY);
            int m = now.get(Calendar.MINUTE);
            int s = now.get(Calendar.SECOND);
            int i = now.get(Calendar.MILLISECOND);
           datetime =(h + ":" + m + ":" + s);
       
            try {
          //  URL sUrl = new URL("http://www.settrade.com/C13_MarketSummary.jsp");
           // System.out.println("sURL " + sUrl);
                
         String buffer;
          String url = "http://www.settrade.com/C13_MarketSummary.jsp",
               proxy = "proxy.up.ac.th",
               port = "8080",
               username = "up\\51286885",
               password = "7489250721";

            //URL autoIP = new URL("http://checkip.dyndns.org:8245/");
            //System.out.println("Get URL");
           
          URL server = new URL(url);

            Authenticator.setDefault(new authenticator(
                        username,password));
            Properties systemProperties = System.getProperties();
            systemProperties.setProperty("http.proxyHost",proxy);
            systemProperties.setProperty("http.proxyPort",port);
            HttpURLConnection connection = (HttpURLConnection)server.openConnection();
            connection.connect();
        // URLConnection conn = sUrl.openConnection();
            PrintStream out;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {
               
                out = new PrintStream(new FileOutputStream("SETINDEXFile.txt"));
                     content.append(datetime).append("\n");
                while((buffer = in.readLine()) != null) {
                       count2++;
                   // avg=(count2/count)*100;
                        
                       content.append(count2).append("-->").append(buffer).append("\n");
                       
                  }
            }
          // //System.out.println("========================");
          // System.out.println(content.toString());
            String index[]={"SET","SET50","SET100"};
            new Readindexstock().readFromFile(content.toString(),"SET");
           System.out.println(Readindexstock.sym[1]);
          // out.println(content.toString());
          //  out.close();
      //  GETINDEXSTOCK.jProgressBar1.setValue(25);
        
      
      //  GETINDEXSTOCK.jProgressBar1.setValue(100);
               
          // System.out.println("========================");
        } catch (IOException | SQLException ez){
            System.err.println("Couldn't create help URL: " + ez);
        

    }// TODO add your handling code here:
           
                
          //  GETThread.jLabel1.setText(String.valueOf(count1));
     
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
        }
         ++a;
        count1++;
        }
    }
    
}
